#ifndef COINMODEL_H
#define COINMODEL_H

#include <QSettings>
#include <QStringList>
#include <QtSql/QSqlTableModel>
#include <QtSql/QSqlDatabase>

class CoinModel : public QSqlTableModel
{
    Q_OBJECT
    Q_ENUMS(View)

    Q_PROPERTY(int count READ rowCount NOTIFY changed)
    Q_PROPERTY(QStringList seriesModel READ seriesModel NOTIFY changed)
    Q_PROPERTY(int series READ series WRITE setSeries NOTIFY changed)
    Q_PROPERTY(QString fileName READ fileName NOTIFY changed)
    Q_PROPERTY(View view READ view WRITE setView NOTIFY changed)


public:
    enum View { CommemorativeWorld, CommemorativeCountry };

    explicit CoinModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"));
    QVariant data(const QModelIndex &index, int role) const;
    Q_INVOKABLE QMap<QString, QVariant> get(int i) const;
    Q_INVOKABLE void setData(int row, const QString & field_name, QVariant new_value);
    Q_INVOKABLE bool open(const QString & file_name);
    Q_INVOKABLE bool update(const QString & file_name);
    Q_INVOKABLE bool isApplied(const QString & date);
    Q_INVOKABLE void clone(int row);
    Q_INVOKABLE void remove(int row);
    Q_INVOKABLE QString folder();
    Q_INVOKABLE QString fileTitle();
    Q_INVOKABLE QString lastCollection();
    int series() const { return m_seriesId; }
    void setSeries(int seriesId);
    QString fileName() { return database().databaseName(); }
    QStringList seriesModel() { return m_seriesModel; }
    CoinModel::View view() const { return m_view; }
    void setView(CoinModel::View view);

    bool select();

signals:
    void changed();

public slots:

private:
    QString makeFilter(const QModelIndex &index, const QString field) const;
    bool isEmtyField(const QModelIndex &index, const QString field) const;
    bool checkVersion();
    bool updateVersion();

private:
    const static char* DEFAULT_FILTER;
    const static char* COLUMN_NAMES[];
    QStringList m_seriesModel;
    int m_seriesId;
    int m_countRoleIndex;
    int m_imgheightRoleIndex;
    int m_imgwidthRoleIndex;
    int m_obverseimgRoleIndex;
    int m_reverseimgRoleIndex;
    QSettings m_settings;
    CoinModel::View m_view;
};

#endif // COINMODEL_H
